<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/25
 * Time: 13:35
 */

namespace App\Game\Traits;


use Carbon\Carbon;

trait AllBetTrait
{
    /**
     * gameapi
     * 2018/9/21 9:43
     * Administrator
     * parseTimeInterval
     * 将时间拆分成最长10分钟的时间段
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function parseTimeInterval(Carbon $startTime, Carbon $endTime)
    {
        $timeArray = [];
        $startMinute = (clone $startTime)->startOfMinute();
        $diffMinutes = $startMinute->diffInMinutes($endTime, false);
        $times = (int)ceil($diffMinutes / 10);

        if ($startTime->diffInMinutes($endTime, false) >= 10) {
            for ($i = 0; $i < $times; $i++) {
                if ($i < $times - 1) {
                    $start = (clone $startTime)->addMinute($i * 10)->format('Y-m-d H:i:s');
                    $end = (clone $startTime)->addMinute(($i + 1) * 10)->format('Y-m-d H:i:s');
                } else {
                    $start = (clone $startTime)->addMinute($i * 10)->format('Y-m-d H:i:s');
                    $end = (clone $endTime)->format('Y-m-d H:i:s');
                }

                $timeArray[] = compact('start', 'end');
            }
        } else {
            $start = (clone $startTime)->format('Y-m-d H:i:s');
            $end = (clone $endTime)->format('Y-m-d H:i:s');
            $timeArray[] = compact('start', 'end');
        }

        return $timeArray;
    }

    public function getBetRecordInDays(Carbon $date)
    {
        $data = [];
        $param = [
            'random' => time(),
            'startDate' => (clone $date)->subDay()->format('Y-m-d'),
            'endDate' => (clone $date)->format('Y-m-d'),
        ];

        $arr = $this->getSign($param);
        $url = $this->gameUrl . "/betlog_daily_histories";
        $params = [
            'data' => $arr['data'], 'sign' => $arr['sign'], 'propertyId' => $this->propertyID
        ];
        $output = $this->request('GET', $url, $params);
        $output = json_decode($output, true);

        if ($output['error_code'] == 'OK') {
            if (count($output['histories']) >= 1) {
                $data = array_merge($data, $output['histories']);
            }
        }

        return $data;
    }

    public function getBetRecordPieceOf(array $times)
    {
        $data = [];
        foreach ($times as $time) {
            $param = [
                'random' => time(),
                'startTime' => $time['start'],
                'endTime' => $time['end'],
            ];

            $arr = $this->getSign($param);
            $url = $this->gameUrl . "/betlog_pieceof_histories_in30days";
            $params = [
                'data' => $arr['data'], 'sign' => $arr['sign'], 'propertyId' => $this->propertyID
            ];
            $output = $this->request('GET', $url, $params);
            $output = json_decode($output, true);

            if ($output['error_code'] == 'OK') {
                if (count($output['histories']) >= 1) {
                    $data = array_merge($data, $output['histories']);
                }
            }
        }
        return $data;
    }
}